home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroTCL3.0 folder / TCL / NeoBench / Source / CRawText.cp < prev   
Encoding:
Text File  |  1993-07-02  |  5.2 KB  |  194 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  CRawText.c
  3.  
  4.                      A Pane containing a text string
  5.  
  6.    This is a text string that can be left justified,  center justified,
  7.    or right justified.    For now,  we can just set it LEFT justified.
  8.  
  9.    The pane's size will be changed depending on the text size.   For
  10.    instance,  when the text is LEFT justified,  the RIGHT side of the
  11.    pane will move out (or in) to just fit the text to be displayed.
  12.  
  13. **********************************************************************/
  14.  
  15. #include "Global.h"
  16. #include "CView.h"
  17. #include "CPane.h"
  18. #include "CBureaucrat.h"
  19. #include "CRawText.h"
  20. #include "TBUtilities.h"
  21. #include <time.h>
  22. #include <stdio.h>
  23.  
  24. /**********************************************************************
  25.  
  26. **********************************************************************/
  27. void    CRawText::IRawText(CView *anEnclosure,  CBureaucrat *aSupervisor,
  28.                            ConstStr255Param theText, short Hencl, short Vencl,
  29.                            short iWidth, short iHeight,
  30.                            short afont,  Style astyle, short asize,
  31.                            short ajustification)
  32. {
  33.     /*
  34.      * We set the pane to have no height or width yet until
  35.      * after we create it.    Then later,  we will change
  36.      * the height and width depending on the font, style, and
  37.      * size chosen as input.
  38.      */
  39.  
  40.     CPane::IPane(anEnclosure, aSupervisor, iWidth, iHeight,
  41.                     Hencl, Vencl, sizFIXEDLEFT, sizFIXEDTOP);
  42.  
  43.     /*
  44.      * Initialize out instance variables,  first set font and
  45.      * size and style,  then set justification.
  46.      */
  47.  
  48.     SetText(theText, ajustification);
  49.     SetTextFont(afont, asize, astyle);
  50.     SetWantsClicks(TRUE);
  51. }
  52.  
  53. /*******************************************************************
  54.  
  55. *******************************************************************/
  56. void    CRawText::JustifyText(short just)
  57. {
  58.  
  59.     FontInfo    fi;
  60.  
  61.     Prepare();
  62.  
  63.     TextFont(font);
  64.     TextFace(style);
  65.     TextSize(size);
  66.  
  67.     /*
  68.      * Now,  we set the text width according to our text
  69.      * and the justification.
  70.      */
  71.  
  72.     textWidth = StringWidth(itsText);
  73.     justification = just;
  74.  
  75.     /*
  76.      * Depending on justification value,   set the start
  77.      * position of the text.
  78.      */
  79.  
  80.  
  81.     GetFontInfo(&fi);
  82.  
  83.     TextPosn.v = fi.ascent + fi.descent + fi.leading;
  84.  
  85.     switch(justification) {
  86.  
  87.         case    teJustLeft:
  88.                 TextPosn.h = 1;
  89.                 break;
  90.  
  91.         case    teJustCenter:
  92.                 if (textWidth > width) {
  93.                     TextPosn.h = 1;            /* Insure first chrs */
  94.                 } else {
  95.                     TextPosn.h = (width - textWidth)/2;
  96.                 }
  97.                 break;
  98.  
  99.         case    teJustRight:
  100.                 if (textWidth > width) {
  101.                     TextPosn.h = 1;
  102.                 } else {
  103.                     TextPosn.h = (width - textWidth) - 3;
  104.                 }
  105.                 break;
  106.     }
  107. }
  108.  
  109. /**********************************************************************
  110.  
  111. **********************************************************************/
  112. void    CRawText::Draw(Rect *area)
  113. {
  114.     TextFont(font);
  115.     TextFace(style);
  116.     TextSize(size);
  117.     MoveTo(TextPosn.h, TextPosn.v - 3);
  118.     DrawString(itsText);
  119. }
  120.  
  121. /********************************************************************
  122.  
  123. *********************************************************************/
  124. void    CRawText::DoClick(Point hitPt, short modifierKeys, long when)
  125. {
  126.     Rect    aFrame;
  127.  
  128.     Prepare();
  129.     SetRect(&aFrame, 0, 0, width-1, height-1);
  130.     FrameRect(&aFrame);
  131. }
  132.  
  133. /*********************************************************************
  134.  
  135. *********************************************************************/
  136. void    CRawText::PutValue(long aValue)
  137. {
  138.     Str255    string;
  139.  
  140.     NumToString(aValue, string);
  141.     SetText(string, teJustRight);
  142.  
  143. }
  144.  
  145. /**********************************************************************
  146.  
  147. **********************************************************************/
  148. void    CRawText::SetText(ConstStr255Param newText, short just)
  149. {
  150.     CopyPString(newText, itsText);
  151.     JustifyText(just);
  152.     Refresh();
  153. }
  154. /*********************************************************************
  155.  
  156. *********************************************************************/
  157. void    CRawText::SetTextFont(short afont, short asize, Style astyle)
  158. {
  159.     font = afont;
  160.     style = astyle;
  161.     size  = asize;
  162.     JustifyText(justification);
  163. }
  164.  
  165.  
  166. /**********************************************************************
  167.  
  168. **********************************************************************/
  169. void    CTimeText::ITimeText(CView *anEnclosure,  CBureaucrat *aSupervisor,
  170.                            ConstStr255Param theText, short Hencl, short Vencl,
  171.                            short iWidth, short iHeight,
  172.                            short afont,  Style astyle, short asize,
  173.                            short ajustification)
  174. {
  175.     IRawText(anEnclosure, aSupervisor, theText, Hencl, Vencl, iWidth, iHeight, afont, astyle, asize, ajustification);
  176. }
  177.  
  178. /*********************************************************************
  179.     aValue is microseconds of time.
  180.  
  181. *********************************************************************/
  182. void    CTimeText::PutValue(long aValue)
  183. {
  184.     unsigned long    value    = (unsigned long)aValue;
  185.     unsigned long    thous    = ((value % 1000000) / 100);    // get thousandths of seconds
  186.     unsigned long    secs    = ((value / 0x100000) % 60);    // get seconds
  187.     unsigned long    mins    = (value / (0x100000 * 60));    // get minutes
  188.     Str255            string;
  189.  
  190.     sprintf((char *)&string[1], "%2.0lu:%2.2lu.%4.4lu", mins, secs, thous);
  191.     string[0] = 11;
  192.     SetText(string, teJustRight);
  193. }
  194.